package com.example.wanandroid.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wanandroid.R;
import com.example.wanandroid.entity.SystemBean;

import java.util.ArrayList;
import java.util.List;

public class SystemAdapter extends RecyclerView.Adapter<SystemAdapter.ViewHolder> {

    private Context mContext;
    private LayoutInflater layoutInflater;
    private List<SystemBean> mList=new ArrayList<>();

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SystemAdapter(Context mContext, List<SystemBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        layoutInflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=layoutInflater.inflate(R.layout.item_system,null);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        SystemBean systemBean=mList.get(i);
        viewHolder.mTextView.setText(systemBean.getName());
        viewHolder.mContent.setText(systemBean.getContent());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
        private TextView mContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mTextView=itemView.findViewById(R.id.mTextView);
            mContent=itemView.findViewById(R.id.mContent);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}
